<?php

/*
 * Created on May 12, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class Tcategory_model extends CI_Model {

    /**
     * @var unknown_type
     */
    var $id;
    /**
     * @var unknown_type
     */
    var $category_parent_id;
    /**
     * @var unknown_type
     */
    var $name_vi;
    /**
     * @var unknown_type
     */
    var $name_en;
    /**
     * @var unknown_type
     */
    var $description_vi;
    /**
     * @var unknown_type
     */
    var $description_en;
    /**
     * @var unknown_type
     */
    var $image;
    /**
     * @var unknown_type
     */
    var $active;
    /*
     * @access	public
     */
    function __construct() {
        parent::__construct();
    }


    function create($category) {
        $sql = "INSERT INTO `gtb_tour_category` " .
                "(`id_parent`, `code`, `code_parent`, " .
                "`vi_name`, `en_name`, `en_description`, `vi_description`, " .
                "`number_tour`, `link_rewrite`, `meta_title`, `meta_keywords`, " .
                "`meta_description`, `level_depth`, `active`, `image`, `date_add`, `date_upd`) " .
                "VALUES";

        $sql .= "(" .
                intval($category['id_parent']) . ", " .
                $this->db->escape($category['code']) . ", " .
                $this->db->escape($category['code_parent']) . ", " .
                $this->db->escape($category['vi_name']) . ", " .
                $this->db->escape($category['en_name']) . ", " .
                $this->db->escape($category['en_description']) . ", " .
                $this->db->escape($category['vi_description']) . ", " .
                intval($category['number_tour']) . ", " .
                $this->db->escape($category['link_rewrite']) . ", " .
                $this->db->escape($category['meta_title']) . ", " .
                $this->db->escape($category['meta_keywords']) . ", " .
                $this->db->escape($category['meta_description']) . "," .
                intval($category['level_depth']) . ", " .
                intval($category['active']) . ", " .
                $this->db->escape($category['image']) . "," .
                $this->db->escape($category['date_add']) . ", " .
                $this->db->escape($category['date_upd']) . "" .
                ")";

        $this->db->query($sql);
        return $this->db->insert_id();
    }

    // multi insert:
    function create2($categories) {

        if (!is_array($categories))
            $categories = array($categories);

        $sql = "INSERT INTO `gtb_pcategory` " .
                "(`id_parent_pcategory`, `code`, `code_parent`, " .
                "`vi_name`, `en_name`, `en_description`, `vi_description`, " .
                "`number_product`, `link_rewrite`, `meta_title`, `meta_keywords`, " .
                "`meta_description`, `level_depth`, `active`, `module`, `image`, `date_add`, `date_upd`) " .
                "VALUES";
        for ($i = 0; $i < count($categories); $i++) {
            $category = $categories[$i];
            $sql .= "(" .
                    intval($category->id_parent_pcategory) . ", " .
                    $this->db->escape($category->code) . ", " .
                    $this->db->escape($category->code_parent) . ", " .
                    $this->db->escape($category->vi_name) . ", " .
                    $this->db->escape($category->en_name) . ", " .
                    $this->db->escape($category->en_description) . ", " .
                    $this->db->escape($category->vi_description) . ", " .
                    intval($category->number_product) . ", " .
                    $this->db->escape($category->link_rewrite) . ", " .
                    $this->db->escape($category->meta_title) . ", " .
                    $this->db->escape($category->meta_keywords) . ", " .
                    $this->db->escape($category->meta_description) . "," .
                    intval($category->level_depth) . ", " .
                    intval($category->active) . ", " .
                    $this->db->escape($category->module) . "," .
                    $this->db->escape($category->image) . "," .
                    $this->db->escape($category->date_add) . ", " .
                    $this->db->escape($category->date_upd) . "" .
                    "),";
        }
        $sql = substr($sql, 0, strlen($sql) - 1);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function update($data) {
        $sql = "UPDATE gtb_tour_category
				SET
				code = " . $this->db->escape($data['code']) . ", " .
                "id_parent = " . intval($data['id_parent']) . "," .
                "code_parent = " . $this->db->escape($data['code_parent']) . "," .
                "vi_name = " . $this->db->escape($data['vi_name']) . "," .
                "en_name = " . $this->db->escape($data['en_name']) . ", " .
                "vi_description = " . $this->db->escape($data['vi_description']) . ", " .
                "en_description = " . $this->db->escape($data['en_description']) . ", " .
                "number_tour = " . intval($data['number_product']) . ", " .
                "link_rewrite = " . $this->db->escape($data['link_rewrite']) . ", " .
                "meta_title = " . $this->db->escape($data['meta_title']) . ", " .
                "meta_keywords = " . $this->db->escape($data['meta_keywords']) . ", " .
                "meta_description = " . $this->db->escape($data['meta_description']) . ", " .
                "level_depth = " . $this->db->escape($data['level_depth']) . ", " .
                "active = " . intval($data['active']) . ", " .
                "best_sell = " . intval($data['best_sell']) . ", " .
                "special = " . intval($data['special']) . ", " .
                "position = " . intval($data['order_view']) . ", " .
                "image = " . $this->db->escape($data['image']) . ", " .
                "date_upd = " . $this->db->escape($data['date_upd']) .
                " WHERE id = " . intval($data['id']);
        $this->db->query($sql);
        $result = $this->db->affected_rows();
        
        if ($result > 0) {
            return;
            $sql = "UPDATE gtb_tour_category " .
                    " SET code_parent = " . $this->db->escape($data['code']) .
                    " WHERE  id_parent = " . intval($data['id']);
            $this->db->query($sql);
            //echo $sql;
           /* $sql = "UPDATE gtb_tour " .
                    " SET code = " . $this->db->escape($data['code']) .
                    " WHERE tour_category_id = " . $this->db->escape($data['id']);
            $this->db->query($sql);
            */
        }

        return $result;
    }

    function applyBestSellProduct($id_pcategory, $value) {
        $sql = 'UPDATE `gtb_product` ' .
                ' SET best_sell =  ' . intval($value) .
                ' WHERE id_pcategory =  ' . intval($id_pcategory);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function applySpecialProduct($id_pcategory, $value) {
        $sql = 'UPDATE `gtb_product` ' .
                ' SET special =  ' . intval($value) .
                ' WHERE id_pcategory =  ' . intval($id_pcategory);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function updateParentId() {
        $sql = 'UPDATE `gtb_pcategory` c1 ' .
                'INNER JOIN `gtb_pcategory` c2 ON c2.code = c1.code_parent ' .
                'SET c1.id_parent_pcategory = c2.id_pcategory ';
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function delete($id) {
        if (!is_array($id))
            $id = array($id);

        //$tmp = array();

        $query = "SELECT id_pcategory, id_parent_pcategory FROM gtb_pcategory";

        $result = $this->db->query($query);

        $tmp = $result->result();

        $categories = array();
        for ($i = 0; $i < count($tmp); $i++) {
            $category = $tmp[$i];
            //$tmp[$category->id_parent_pcategory][$category->id_pcategory] = $category;
            $categories[$category->id_parent_pcategory][$category->id_pcategory] = $category;
        }

        $tmp = array();

        for ($i = 0; $i < count($id); $i++) {
            $childs = getElementsOfNode($id[$i], $categories);
            $tmp = array_merge($tmp, $childs);
        }
        $tmp = array_merge($tmp, $id);
        //print_r($tmp);

        $ids = implode(',', $tmp);

        // Delete product
        $sql = "SELECT id_product, image FROM gtb_product WHERE id_pcategory IN ($ids)";
        $query = $this->db->query($sql);
        $result = $query->result();
        $ids_product = array();

        $images_product = array();
        for ($i = 0; $i < count($result); $i++) {
            $ids_product[] = $result[$i]->id_product;
            if ($result[$i]->image)
                $images_product[] = $result[$i]->image;
        }

        $ids_fck = array();
        if (count($ids_product)) {

            //$ids_product = implode(',', $ids_product);
            // Xoa hinh anh:
            $sql = "SELECT id, file_url FROM gtb_fck_files WHERE `type` = 'product' AND item_id IN (" . implode(',', $ids_product) . ")";
            $query = $this->db->query($sql);
            $tmp = $query->result();
            $ids_fck = array();
            for ($i = 0; $i < count($tmp); $i++) {
                $ids_fck[] = $tmp[$i]->id;
                $images_product[] = $tmp[$i]->file_url;
            }
            //$ids_fck = implode(',', $ids_fck);
        }

        $sql = "DELETE FROM gtb_pcategory WHERE id_pcategory IN ($ids)";
        $this->db->query($sql);
        $result = $this->db->affected_rows();
        $affected_rows = $result;
        if ($result AND count($ids_product) > 0) {
            // Xoa san pham
            $sql = "DELETE FROM gtb_product WHERE id_product IN (" . implode(',', $ids_product) . ")";
            $this->db->query($sql);
            $result = $this->db->affected_rows();
            $affected_rows += $result;
            if ($result AND count($ids_fck) > 0) {
                $sql = "DELETE FROM gtb_fck_files WHERE `id` IN (" . implode(',', $ids_fck) . ")";
                $this->db->query($sql);
                $result = $this->db->affected_rows();
                $affected_rows += $result;
                for ($i = 0; $i < count($images_product); $i++) {
                    @unlink($images_product[$i]);
                }
            }

            // Xoa bang gia:
            $sql = "SELECT file_name FROM gtb_files WHERE id_category IN ($ids)";
            $query = $this->db->query($sql);
            $tmp = $query->result();
            for ($i = 0; $i < count($tmp); $i++) {
                @unlink($tmp[$i]->file_name);
            }
            $sql = "DELETE FROM gtb_files WHERE id_category IN ($ids)";
            $query = $this->db->query($sql);
            $result = $this->db->affected_rows();
            $affected_rows += $result;
        }

        return $affected_rows;
    }

    function getAllByCode($code_parent = '', $number_record = 0, $offset = 0) {
        $result = null;

        if ($number_record > 0) {
            $sql = "SELECT id, id_parent, code_parent, code,vi_name,en_name, vi_description, active
					FROM gtb_tour_category
					WHERE (code_parent = '" . trim($code_parent) . "' )
					ORDER BY order_view ASC
					LIMIT $offset, $number_record";
        }

        else
            $sql = "SELECT id, id_parent, code_parent, code,vi_name,en_name, vi_description
				FROM gtb_pcategory ORDER BY order_view ASC";
        //echo $sql;
        $query = $this->db->query($sql);
        $result = $query->result();
        $query->free_result();
        return $result;
    }

    function getAll($parent = -1) {

        $sql = "SELECT *
		FROM gtb_tour_category WHERE id_parent=$parent OR -1 = $parent ORDER BY position ASC";

        //echo $sql;
        $query = $this->db->query($sql);
        $result = $query->result();
        $query->free_result();
        return $result;
    }

    function countAll($code_parent) {
        $result = null;

        $sql = "SELECT COUNT(*) as num_record
					FROM gtb_tour_category
					WHERE (code_parent = '" . trim($code_parent) . "'  )";

        $query = $this->db->query($sql);
        $result = $query->result();
        $query->free_result();
        return $result[0]->num_record;
    }

    function getByParentCode($code_parent) {
        $result = null;
        $sql = "SELECT *
				FROM gtb_tour_category WHERE code_parent = " . $this->db->escape($code_parent) . " ORDER BY position ASC";

        $query = $this->db->query($sql);
        $result = $query->result();
        $query->free_result();
        return $result;
    }

    function getMaxPosition($category_parent_id) {

        $sql = "SELECT MAX(position) as position_max
				FROM category
				WHERE category_parent_id = $category_parent_id";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->position_max;
        }
        $query->free_result();
        return 0;
    }

    function getById($category_id) {

        $sql = "SELECT *
		FROM gtb_tour_category
		WHERE id = " . intval($category_id);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        $query->free_result();
        return null;
    }

    function getAllIDByParentId($parent_id) {

        $sql = "SELECT id
		FROM gtb_tour_category
		WHERE id_parent = " . intval($parent_id);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result;
        }

        $query->free_result();
        return null;
    }

    function getByCode($code) {
        $sql = "SELECT *
		FROM gtb_tour_category
		WHERE code = " . $this->db->escape($code);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

    function getByLinkRewrite($link_rewrite) {

        $sql = "SELECT *
				FROM gtb_tour_category
				WHERE link_rewrite = " . $this->db->escape($link_rewrite);
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0];
        }

        return null;
    }

    function getAllAdmin($offset, $limit, $id_parent_pcategory) {
        /* $where = ' WHERE ';
          foreach($parameter as $para_name => $value){
          $where .= ' ' . $para_name . '=' . $this->db->escape($value) . ',';
          }

          $where = substr($where, 0, strlen($where)-1);

          if(count($parameter) == 0) $where = '';
         */
        $sql = "SELECT a.*," .
                " (SELECT MAX(t3.position) FROM gtb_tour_category t3) as max_order,
					(SELECT MIN(t4.position) FROM gtb_tour_category t4) as min_order,
					(SELECT (t5.id) FROM gtb_tour_category t5 WHERE t5.position > a.position AND id_parent = " . intval($id_parent_pcategory) . " LIMIT 0,1) as id_next,
					(SELECT (t6.id) FROM gtb_tour_category t6 WHERE t6.position < a.position AND id_parent = " . intval($id_parent_pcategory) . " ORDER BY t6.position DESC LIMIT 0,1) as id_preview " .
                " FROM gtb_tour_category a" .
                " WHERE id_parent = " . intval($id_parent_pcategory) .
                " ORDER BY a.position ASC" .
                " LIMIT " . intval($offset) . ", " . intval($limit);

        //echo $sql;
        $query = $this->db->query($sql);
        $result = $query->result();
        $query->free_result();
        return $result;
    }

    function countAllAdmin($id_parent_pcategory) {
        $result = null;

        /* $where = ' WHERE ';
          foreach($parameter as $para_name => $value){
          $where .= ' ' . $para_name . '=' . $this->db->escape($value) . ',';
          }

          $where = substr($where, 0, strlen($where)-1);

          if(count($parameter) == 0) $where = '';
         */
        $sql = "SELECT COUNT(*) as num_record
				FROM gtb_tour_category
				WHERE id_parent = " . intval($id_parent_pcategory);

        $query = $this->db->query($sql);
        $result = $query->result();
        $query->free_result();
        return $result[0]->num_record;
    }

//
}

// EOF

